package com.swss.g.sys.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.swss.g.base.impl.BaseDAOImpl;
import com.swss.g.sys.bean.GBygl;
import com.swss.g.sys.dao.ByglDAO;

public class ByglDAOImpl extends BaseDAOImpl<GBygl> implements ByglDAO{

	@SuppressWarnings("unchecked")
	public List findAllStandField() throws Exception {
		// TODO Auto-generated method stub
		String hql="FROM GBygl ORDER BY id ASC";
		try{
		return this.findByHql(hql);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public boolean updateStandFields(final List<GBygl> bys) throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						try {
							for (GBygl b : bys) {
								GBygl by = (GBygl) session.get(GBygl.class, b.getId());
								by.setNewField(b.getNewField());
								by.setDefaultVal(b.getDefaultVal());

								session.update(by);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception ex) {

							ex.printStackTrace();
							return false;
						}
					}
				});
		return flag;
	}
}
